﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using YY.BLL;

namespace YY.Web.MyAgent
{
    public partial class BonusSet : BaseFunction.LoginHelp
    {
        protected Model.agent_fund_setting modelAgentFundSetting;
        protected BLL.agent_fund_setting bllAgentFundSetting = new agent_fund_setting();
        protected Model.agent_users modelAgentUsers;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();
            }
        }
        private void InitData()
        {
            modelAgentUsers = base.GetSessionModel();
            hf_userid.Value = modelAgentUsers.id + "";
            modelAgentFundSetting = bllAgentFundSetting.GetModel(modelAgentUsers.id);
            if (modelAgentFundSetting != null)
            {
                string settingtemplate = modelAgentFundSetting.sales_performance;
                settingtemplate = System.Web.HttpUtility.HtmlDecode(settingtemplate); //从数据库中查出来需要解码
                settingtemplate = settingtemplate.Replace(" ", "").Trim(); //数据库中查出来有时候,会在引号后出现空格
                hf_settingtemplate.Value = settingtemplate;
                JObject Object_jsondata = Newtonsoft.Json.JsonConvert.DeserializeObject(settingtemplate) as JObject;
                string allmoneysettings = Object_jsondata["allmoneysettings"] + "";
                List<MoneyHelper> linkLists = JsonConvert.DeserializeObject<List<MoneyHelper>>(allmoneysettings);
                rpt_links.DataSource = linkLists;
                rpt_links.DataBind();
            }
            else
            {
                string settingtemplate = "{\"allmoneysettings\":[]}";
                hf_settingtemplate.Value = settingtemplate;
            }
        }

        //protected void btnSubmit_Click(object sender, EventArgs e)
        //{
        //    if (modelAgentFundSetting != null) //update
        //    {
        //        //string sales_money = this.txtMinMoney.Text + "," + this.txtMaxMoney.Text;
        //        //modelAgentFundSetting.sales_performance = sales_money;
        //        //modelAgentFundSetting.percent = this.txtShare.Text;
        //        //if (bllAgentFundSetting.Update(modelAgentFundSetting))
        //        //    BaseFunction.MessageBox.ResponseScript(this.Page,
        //        //        string.Format("parent.jsprint(\"更新分红配置成功！\",\"\")"));
        //        //else
        //        //    BaseFunction.MessageBox.ResponseScript(this.Page, "parent.jsprint(\"更新分红配置失败！\"");
        //    }
        //    else//add
        //    {
        //        //Model.agent_fund_setting model_agent_fund_setting = new Model.agent_fund_setting();
        //        //modelAgentUsers = base.GetSessionModel();
        //        //model_agent_fund_setting.id = modelAgentUsers.id;
        //        //string sales_money = this.txtMinMoney.Text + "," + this.txtMaxMoney.Text;
        //        //model_agent_fund_setting.sales_performance = sales_money;
        //        //model_agent_fund_setting.percent = this.txtShare.Text;
        //        //if (bllAgentFundSetting.Add(model_agent_fund_setting) > 0)
        //        //    BaseFunction.MessageBox.ResponseScript(this.Page,
        //        //       string.Format("parent.jsprint(\"添加分红配置成功！\",\"\")"));
        //        //else
        //        //    BaseFunction.MessageBox.ResponseScript(this.Page, "parent.jsprint(\"添加分红配置失败！\"");
        //    }
        //}
        [WebMethod]
        public static int Save(string jsondata, string userid)
        {
            BLL.agent_fund_setting bll_agentFundSetting = new agent_fund_setting();
            Model.agent_fund_setting model_agentFundSetting = bll_agentFundSetting.GetModel(Convert.ToInt32(userid));
            //编码后 再存入数据库
            jsondata = System.Web.HttpUtility.HtmlEncode(jsondata);
            if (model_agentFundSetting == null)
            {
                model_agentFundSetting = new Model.agent_fund_setting();
                model_agentFundSetting.id = Convert.ToInt32(userid);
                model_agentFundSetting.sales_performance = jsondata;
                if (bll_agentFundSetting.Add(model_agentFundSetting) > 0)
                    return 1;
                else
                    return 0;
            }
            else
            {
                model_agentFundSetting.sales_performance = jsondata;
                if (bll_agentFundSetting.Update(model_agentFundSetting))
                    return 1;
                else
                    return 0;
            }
        }
        public class MoneyHelper
        {
            public string money { get; set; }
            public string share { get; set; }
        }
    }
}